const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
  showMsg
} from '../../../../utils/config.js'
import {
  validateEmoji
} from '../../../../utils/validate.js'
let spaceDel = require('../behaviors/space/space-del.js')
let behaviorsOrgSelection = require('../../../../pages/behaviors/org/orgSelection.js')
import {
  reqSpaceList,
  reqSpaceStatistics,
  reqSpaceDefence
} from '../../../../request/api'
Page({
  behaviors: [spaceDel, behaviorsOrgSelection],
  data: {
    navHeight: app.globalData.navHeight + 'px', //头部距离
    bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
    titleTip: '建筑物', //删除时提示
    parkList: [], //园区列表
    // parkItemInfo: {}, //选中的一条园区xinxi
    statistics: {
      construction: 0,
      floor: 0,
      space: 0,
    },
    dataList: [], //建筑物列表
    page: 1,
    searchValue: '', //输入的搜索值
    timer: '', //搜索值定时
    isNextPage: true, //是否下一页
    triggered: false, //是否下拉刷新列表

    // 更多操作开始
    showMoreActions: false, //是否展示更多操作
    listItemInfo: {}, //点击更多时列表一项信息
    // 更多操作结束
  },
  onLoad() {
    // this.getInitParkList()
    // this.getDataList()
    // this.getStatistics()
  },
  onShow() {
    this.getStatistics()
    this.getInitDataList()
  },

  // 机构选择确定
  confirmOrg() {
    this.getStatistics()
    this.getInitDataList()
  },
  // 单位弹窗确定
  confirmUnit() {
    this.getStatistics()
    this.getInitDataList()
  },

  // 获取统计数据
  async getStatistics() {
    const res = await reqSpaceStatistics({
      tenantId: app.globalData.tenantId,
      orgId: this.data.orgInfo.orgId,
      userId: app.globalData.userId,
      roleName: app.globalData.rolename,
      pid: 0,
    })
    this.setData({
      statistics: res.data.row
    })
  },
  /**
   * @name: 获取建筑物列表
   * @msg: *parkId：园区id  *pid：父id  name：空间名称
   * @return {*}
   */
  async getDataList() {
    let params = {
      tenantId: app.globalData.tenantId,
      orgId: this.data.orgInfo.orgId,
      pid: '0',
      name: this.data.searchValue,
      userId: app.globalData.userId,
      roleName: app.globalData.rolename,
    }
    const res = await reqSpaceList(this.data.page, 10, params)
    if (res.code !== 20000) return showMsg(res.message)
    let {
      rows
    } = res.data
    if (this.data.page === 1) {
      this.setData({
        dataList: rows,
        isNextPage: true
      })
    } else if (this.data.page > 1 && rows.length) {
      this.setData({
        dataList: this.data.dataList.concat(rows),
        isNextPage: true
      })
    } else if (this.data.page > 1 && !rows.length) {
      showMsg('已经到底啦~')
      this.setData({
        isNextPage: false
      })
    }
  },
  // 滚动到底部触发
  lower() {
    if (!this.data.isNextPage) return
    this.setData({
      page: this.data.page + 1,
    })
    this.getDataList()
  },
  // 下拉刷新
  async onRefresh() {
    this.setData({
      dataList: [],
      page: 1,
      isNextPage: true,
      triggered: true,
    })
    await this.getDataList()
    this.setData({
      triggered: false,
    })
  },
  // 输入框搜索
  searchChange(e) {
    this.data.searchValue = validateEmoji(e.detail.replace(/\s+/g, ''))
    this.setData({
      searchValue: this.data.searchValue,
      page: 1,
      isNextPage: true
    })
    clearTimeout(this.data.timer)
    this.data.timer = setTimeout(() => {
      this.getDataList()
    }, 1000)
  },
  // 操作列表后回到最初状态
  getInitDataList() {
    this.setData({
      page: 1,
      isNextPage: true,
      dataList: []
    })
    this.getDataList()
  },
  // 点击布撤防
  changeDefence(e) {
    let {
      item
    } = e.currentTarget.dataset
    let tip = ''
    if (e.detail) {
      tip = '此操作将开启该建筑物所有设备布防设置，是否继续？'
      item.isDefence = 1
    } else {
      tip = '此操作将关闭该建筑物所有设备布防设置，是否继续？'
      item.isDefence = 0
    }
    Dialog.confirm({
        title: '温馨提示',
        message: tip,
      })
      .then(async () => {
        const res = await reqSpaceDefence({
          spaceId: item.id,
          isDefence: item.isDefence
        })
        if (res.code !== 20000) return showMsg(res.message)
        showMsg("设置成功", 'success')
        this.getInitDataList()
      })
      .catch(() => {});
  },
  changeDefence2() {
    Dialog.confirm({
        title: '温馨提示',
        message: '布防将对该建筑物下所有楼层及安装点同时进行布防，确认后将更新该建筑物下所有楼层及安装点为布防状态！',
      })
      .then(async () => {
        const res = await reqSpaceDefence({
          spaceId: this.data.listItemInfo.id,
          isDefence: 1
        })
        this.unShowMoreActions()
        if (res.code !== 20000) return showMsg(res.message)
        // this.getInitDataList()
        showMsg("设置成功")
      })
      .catch(() => {});
  },
  changeDefence3() {
    Dialog.confirm({
        title: '温馨提示',
        message: '撤防将对该建筑物下所有楼层及安装点同时进行撤防，确认后将更新该建筑物下所有楼层及安装点为撤防状态！',
      })
      .then(async () => {
        const res = await reqSpaceDefence({
          spaceId: this.data.listItemInfo.id,
          isDefence: 0
        })
        this.unShowMoreActions()
        if (res.code !== 20000) return showMsg(res.message)
        // this.getInitDataList()
        showMsg("设置成功")
      })
      .catch(() => {});
  },
  // 点击更多操作
  clickMore(e) {
    let {
      item
    } = e.currentTarget.dataset
    this.setData({
      listItemInfo: item,
      showMoreActions: true
    })
  },
  // 点击xx
  closeMoreActions() {
    this.unShowMoreActions()
  },
  // 隐藏更多操作弹窗
  unShowMoreActions() {
    this.setData({
      showMoreActions: false
    })
  },
  // 点击去编辑
  clickEdit() {
    let {
      listItemInfo
    } = this.data
    let item = JSON.stringify(listItemInfo)
    wx.navigateTo({
      url: `/packageA/pages/index-inside-page/building-edit/building-edit?item=${item}`,
    })
    this.unShowMoreActions()
  },
  // 去新增建筑物
  goAddBuilding() {
    // let {
    //     id: parkId
    // } = this.data.parkItemInfo
    wx.navigateTo({
      url: `/packageA/pages/index-inside-page/building-add/building-add`,
    })
  },
  // 去楼层
  goNextSpace(e) {
    console.log(e);
    let {
      id: pid,
      parkId,
      name: pName,
      orgId
    } = e.currentTarget.dataset.item
    let info = {
      pid,
      parkId,
      pName,
      orgId
    }
    let params = JSON.stringify(info)
    wx.navigateTo({
      url: `/packageA/pages/index-inside-page/floor/floor?params=${params}`,
    })
  }
})